package com.xyz.rok.tools.model;

import com.xyz.rok.tools.constant.SystemCode;

import java.io.Serializable;

public class Result<T> implements Serializable {

    private static final long serialVersionUID = -5809782578272943999L;

    private String msg;
    private String code;
    private T data;

    public Result() {
    }

    private Result(T data) {
        this.code = SystemCode.SYS_API_SUCCESS.getCode();
        this.msg = SystemCode.SYS_API_SUCCESS.getMessage();
        this.data = data;
    }

    private Result(SystemCode cm) {
        if (cm == null) {
            return;
        }
        this.code = cm.getCode();
        this.msg = cm.getMessage();
    }

    private Result(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private Result(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    private Result(SystemCode cm, T data) {
        if (cm == null) {
            return;
        }
        this.code = cm.getCode();
        this.msg = cm.getMessage();
        this.data = data;
    }

    /**
     * 成功时候的调用
     *
     * @return
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>(data);
    }

    /**
     * 成功，不需要传入参数
     *
     * @return
     */
    public static <T> Result<T> success() {
        return (Result<T>) success(null);
    }


    public static <T> Result<T> error(String code, String msg) {
        return new Result<T>(code, msg);
    }

    public static <T> Result<T> error(SystemCode code) {
        return new Result<T>(code.getCode(), code.getMessage());
    }

    public static <T> Result<T> error(String code, String msg, T data) {
        return new Result<T>(code, msg, data);
    }

    public T getData() {
        return data;
    }

    public String getMsg() {
        return msg;
    }

    public String getCode() {
        return code;
    }
}